package com.generator;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.util.List;

/**
 * A MyBatis Generator plugin to use Lombok's @Data annoation
 * instead of getters and setters
 *
 * @author Paolo Predonzani (http://softwareloop.com/)
 */
public class MybatisPlusPlugin extends PluginAdapter {

    private FullyQualifiedJavaType tableField;
    private FullyQualifiedJavaType tableId;
    private FullyQualifiedJavaType tableName;
    private FullyQualifiedJavaType tersion;
    private FullyQualifiedJavaType tableLogic;

    /**
     * LombokPlugin contructor
     */
    public MybatisPlusPlugin() {
        tableField = new FullyQualifiedJavaType("com.baomidou.mybatisplus.annotation.TableField");
        tableId = new FullyQualifiedJavaType("com.baomidou.mybatisplus.annotation.TableId");
        tableName = new FullyQualifiedJavaType("com.baomidou.mybatisplus.annotation.TableName");
        tersion = new FullyQualifiedJavaType("com.baomidou.mybatisplus.annotation.Version");
        tableLogic = new FullyQualifiedJavaType("com.baomidou.mybatisplus.annotation.TableLogic");
    }

    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }

    /**
     * Intercepts base record class generation
     *
     * @param topLevelClass
     * @param introspectedTable
     * @return
     */
    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
                                                 IntrospectedTable introspectedTable) {
        topLevelClass.addImportedType(tableField);
        topLevelClass.addAnnotation("@TableName(\""+introspectedTable.getFullyQualifiedTableNameAtRuntime()+"\")");

        topLevelClass.addImportedType(tableId);
        topLevelClass.addImportedType(tableName);
        topLevelClass.addImportedType(tersion);
        topLevelClass.addImportedType(tableLogic);
        return true;
    }

    @Override
    public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        topLevelClass.addImportedType(tableId);
        return super.modelPrimaryKeyClassGenerated(topLevelClass, introspectedTable);
    }

    @Override
    public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        if(introspectedTable.getPrimaryKeyColumns().contains(introspectedColumn)){
            field.addAnnotation("@TableId(\""+introspectedColumn.getActualColumnName()+"\")");
        }else{
            if(introspectedColumn.getActualColumnName().trim().equals("c_revision")){
                field.addAnnotation("@Version");
            }else if(introspectedColumn.getActualColumnName().trim().equals("c_delete_flag")){
                field.addAnnotation("@TableLogic(value=\"0\",delval=\"1\")");
            }
            field.addAnnotation("@TableField(\""+introspectedColumn.getActualColumnName()+"\")");
        }
        return super.modelFieldGenerated(field, topLevelClass, introspectedColumn, introspectedTable, modelClassType);
    }
}